package cn.edu.zjgsu.ito.util;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;

import org.apache.commons.io.FileUtils;
import org.springframework.web.multipart.MultipartFile;

public class FileUtil
{
	public static String getRandomFileName()
	{
		StringBuffer sb = new StringBuffer(32);
		String timeStr = new SimpleDateFormat("yyyyMMddhhmmssmm").format(System
				.currentTimeMillis());
		sb.append("/photo/");
		sb.append(timeStr);
		sb.append(((int) (Math.random() * 1000) + 1000));
		return sb.toString();
	}

	public static void savePhoto(MultipartFile photo, String filename)
			throws IOException
	{
		InputStream inputStream = photo.getInputStream();
		File file = new File(filename);
		if (!file.exists())
		{
			file.createNewFile();
		}
		FileUtils.copyInputStreamToFile(inputStream, file);
	}
}
